package br.com.authentication.transaction.impl;

import br.com.authentication.authenticator.TransactionAuthenticator;
import br.com.authentication.exception.AuthenticationException;
import br.com.authentication.exception.AuthenticatorException;
import br.com.authentication.exception.TransactionException;
import br.com.authentication.session.UserSession;
import br.com.authentication.transaction.ITransaction;

public abstract class ValuableTransaction implements ITransaction {

	private UserSession userSession;

	protected abstract void accomplishTransaction() throws TransactionException;

	protected abstract void forsakeTransaction();

	public void execute() throws AuthenticatorException {
		try {
			authenticateTransaction();
			accomplishTransaction();
		} catch (TransactionException e) {
			forsakeTransaction();
		}
	}

	private void authenticateTransaction() throws AuthenticationException {
		TransactionAuthenticator.getInstance().authenticate(getUserSession());
	}

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
}
